package edu.wpi.cs509.representation;

import static org.junit.Assert.fail;

import java.util.logging.Logger;

import org.junit.Test;

public class ActionTest2 {



	//Game clientGame;  // the game
	Gameboard clientGameboard; // the gameboard

	Game serverGame;  // the game
	Gameboard serverGameboard; // the gameboard

	Location loc; // a location
	PlayerPiece piece, piece2; // two pieces

	ActionBaseClass theAction;  // a base class action



	/**
	 * The main test routine.
	 */
	@Test(timeout=5000)
	public void testTheAction() {
		//		int xCoord = 1;
		//		int yCoord = 0;
		//		boolean isRed = (yCoord <=4);
		String NEW_LINE = System.getProperty("line.separator");

		// generate game, gameboard
		//clientGame = new Game();
		//clientGameboard = clientGame.getGameboard();

		// generate game, gameboard
		serverGame = new Game();
		serverGameboard = serverGame.getGameboard();


		///////////////////////
		// print the gameboards
		//Logger.getLogger("global").info("clientGameboard"+NEW_LINE+clientGameboard.toString());
		//Logger.getLogger("global").info("serverGameboard"+NEW_LINE+serverGameboard.toString());


		//////////////////////////////////////////////
		//////////////////////////////////////////////

		for (int loop=0; loop<=3; loop++) {
			Logger.getLogger("global").info("------------- loop "+loop+"-------------");

			// the client gameboard gets updated to reflect the server gameboard
			clientGameboard = serverGameboard;
			
			// loop 0,1 add pieces (one red, one blue)
			if ((loop==0) || (loop==1)) {
				////////////////////
				// add a piece
				Logger.getLogger("global").info("--- add a piece");
				piece = clientGameboard.getPlayerPieceInBoneyard(
						((loop==0) ? TeamColor.RED       : TeamColor.BLUE),
						((loop==0) ? StrategoPiece.SCOUT : StrategoPiece./*SCOUT*/ /*SPY*/ LIEUTENANT));
				try {
					loc = clientGameboard.getBoardsquare(
							((loop==0) ? 1 : 1),  // x lines up
							((loop==0) ? 0 : 9)); // y on proper side of board
				} catch (GameboardException e) {
					e.printStackTrace();
					fail("100.0 - caught an exception");
				}

				if (piece==null)
					fail("piece is null");
				if (loc==null)
					fail("loc is null");	

				// check the move against client gameboard

				ActionAdd aAdd = new ActionAdd("add piece", clientGameboard, piece, loc);
				try {
					aAdd.strategoRulesCheck();
				} catch (ActionNotLegalException e) {
					e.printStackTrace();
					fail("101.2 - caught an exception");
				}
				theAction = aAdd;  // upclass it
			}

			// loops 2,3 performs a move then an attack using the Scout
			else if ((loop==2) || (loop==3)) {
				////////////////////
				// move piece
				Logger.getLogger("global").info("--- move piece");
				try {
					piece = clientGameboard.getPlayerPieceOnBoardSquare(
							((loop==2) ? 1 : 1),
							((loop==2) ? 0 : 8));
				} catch (GameboardException e1) {
					e1.printStackTrace();
					fail("100.0 - caught an exception");
				}
				try {
					loc = clientGameboard.getBoardsquare(
							((loop==2) ? 1 : 1),
							((loop==2) ? 8 : 9));
				} catch (GameboardException e) {
					e.printStackTrace();
					fail("100.0 - caught an exception");
				}

				if (piece==null)
					fail("piece is null");
				if (loc==null)
					fail("loc is null");	

				// check the move against client gameboard
				ActionMove aMove = new ActionMove("move piece", clientGameboard, piece, loc);
				try {
					aMove.strategoRulesCheck();
				} catch (ActionNotLegalException e) {
					e.printStackTrace();
					fail("101.2 - caught an exception");
				}
				theAction = aMove;  // upclass it	
			}

			///////////////////////
			// print the gameboards
			Logger.getLogger("global").info("clientGameboard before execute"+NEW_LINE+clientGameboard.toString());
			Logger.getLogger("global").info("serverGameboard before execute"+NEW_LINE+serverGameboard.toString());

			// switch the Action to the server gameboard	
			theAction.setGameboard(serverGameboard);

			// update the server gameboard (run execute)
			try {
				theAction.execute();
			} catch (ActionEndOfGameException e) {
				e.printStackTrace();
				fail("101.1 - caught an exception");
			} catch (ActionNotLegalException e) {
				e.printStackTrace();
				fail("101.2 - caught an exception");
			}

			///////////////////////
			// print the gameboards
			Logger.getLogger("global").info("clientGameboard after execute"+NEW_LINE+clientGameboard.toString());
			Logger.getLogger("global").info("serverGameboard after execute"+NEW_LINE+serverGameboard.toString());

			// print the historical winners and losers
			Logger.getLogger("global").info("------winning pieces-------");
			for (PlayerPiece winnerPiece : theAction.getHistoricalWinningPiecesList()) {
				Logger.getLogger("global").info(winnerPiece.toString());
			}
			Logger.getLogger("global").info("------losing pieces-------");
			for (PlayerPiece loserPiece : theAction.getHistoricalLosingPiecesList()) {
				Logger.getLogger("global").info(loserPiece.toString());
			}
			Logger.getLogger("global").info("------------------------");
		}
	}
}
